import tkinter as tk
from tkinter import messagebox
from ..widgets.animated_button import create_animated_button, create_icon_button
from ..styles.colors import BACKGROUNDS, TEXT_COLORS, CLASS_COLORS
from ..styles.fonts import get_font

PRESET_CLASSES = ["战士", "法师", "弓箭手", "刺客", "牧师", "其他"]

def show_creation_screen(parent, items, on_back, on_character_created):
    for widget in parent.winfo_children():
        widget.destroy()
    
    main_container = tk.Frame(parent, bg=BACKGROUNDS["main"])
    main_container.pack(fill="both", expand=True)
    
    header_frame = tk.Frame(main_container, bg=BACKGROUNDS["header"], height=60)
    header_frame.pack(fill="x", pady=(0, 20))
    header_frame.pack_propagate(False)
    
    title_label = tk.Label(
        header_frame,
        text="✨ 创建新英雄",
        fg=TEXT_COLORS["accent"],
        bg=BACKGROUNDS["header"],
        font=get_font("header", "bold")
    )
    title_label.pack(side="left", padx=20, pady=20)
    
    create_animated_button(
        header_frame,
        "返回",
        on_back,
        style="primary",
        width=8
    ).pack(side="right", padx=20, pady=20)
    
    content_frame = tk.Frame(main_container, bg=BACKGROUNDS["main"])
    content_frame.pack(fill="both", expand=True, padx=50)
    
    form_frame = tk.Frame(content_frame, bg=BACKGROUNDS["main"])
    form_frame.pack(expand=True)
    
    # 名称输入
    name_frame = tk.Frame(form_frame, bg=BACKGROUNDS["main"])
    name_frame.pack(fill="x", pady=15)
    
    tk.Label(
        name_frame,
        text="英雄名称:",
        fg=TEXT_COLORS["primary"],
        bg=BACKGROUNDS["main"],
        font=get_font("normal", "bold")
    ).pack(anchor="w")
    
    name_entry = tk.Entry(
        name_frame,
        bg=BACKGROUNDS["input"],
        fg=TEXT_COLORS["primary"],
        font=get_font("normal"),
        width=30
    )
    name_entry.pack(fill="x", pady=5)
    name_entry.focus()
    
    # 职业选择
    class_frame = tk.Frame(form_frame, bg=BACKGROUNDS["main"])
    class_frame.pack(fill="x", pady=15)
    
    tk.Label(
        class_frame,
        text="职业选择:",
        fg=TEXT_COLORS["primary"],
        bg=BACKGROUNDS["main"],
        font=get_font("normal", "bold")
    ).pack(anchor="w")
    
    class_choice_frame = tk.Frame(class_frame, bg=BACKGROUNDS["main"])
    class_choice_frame.pack(fill="x", pady=5)
    
    class_var = tk.StringVar(value="战士")
    
    for i, class_name in enumerate(PRESET_CLASSES):
        color = CLASS_COLORS.get(class_name, CLASS_COLORS["其他"])
        btn = tk.Radiobutton(
            class_choice_frame,
            text=class_name,
            variable=class_var,
            value=class_name,
            fg=color,
            bg=BACKGROUNDS["main"],
            selectcolor=BACKGROUNDS["input"],
            activebackground=BACKGROUNDS["main"],
            activeforeground=color,
            font=get_font("normal")
        )
        btn.pack(side="left", padx=(0, 15))
    
    # 等级输入
    level_frame = tk.Frame(form_frame, bg=BACKGROUNDS["main"])
    level_frame.pack(fill="x", pady=15)
    
    tk.Label(
        level_frame,
        text="英雄等级 (1-100):",
        fg=TEXT_COLORS["primary"],
        bg=BACKGROUNDS["main"],
        font=get_font("normal", "bold")
    ).pack(anchor="w")
    
    level_input_frame = tk.Frame(level_frame, bg=BACKGROUNDS["main"])
    level_input_frame.pack(fill="x", pady=5)
    
    level_var = tk.StringVar(value="1")
    
    level_entry = tk.Entry(
        level_input_frame,
        textvariable=level_var,
        bg=BACKGROUNDS["input"],
        fg=TEXT_COLORS["primary"],
        font=get_font("normal"),
        width=10
    )
    level_entry.pack(side="left")
    
    # 按钮区域
    button_frame = tk.Frame(form_frame, bg=BACKGROUNDS["main"])
    button_frame.pack(pady=30)
    
    def create_character():
        name = name_entry.get().strip()
        class_name = class_var.get()
        level_str = level_var.get()
        
        if not name:
            messagebox.showerror("错误", "请输入英雄名称！")
            name_entry.focus()
            return
        
        try:
            level = int(level_str)
            if level < 1 or level > 100:
                messagebox.showerror("错误", "等级必须在1-100之间！")
                level_entry.focus()
                return
        except ValueError:
            messagebox.showerror("错误", "请输入有效的等级数字！")
            level_entry.focus()
            return
        
        if len(items) >= 4:
            messagebox.showerror("错误", "已达到最大角色数量（4个）！")
            return
        
        on_character_created(name, class_name, level)
    
    create_btn = create_icon_button(
        button_frame,
        "创建英雄",
        create_character,
        "⭐",
        "success"
    )
    create_btn.config(width=20, height=2)
    create_btn.pack()
    
    tip_label = tk.Label(
        button_frame,
        text="💡 提示: 最多可以创建4个角色",
        fg=TEXT_COLORS["secondary"],
        bg=BACKGROUNDS["main"],
        font=get_font("small")
    )
    tip_label.pack(pady=10)
    
    def on_enter_key(event):
        create_character()
    
    parent.bind('<Return>', on_enter_key)
    
    def cleanup():
        parent.unbind('<Return>')
    
    main_container.cleanup = cleanup
    
    return main_container